package mycrmprkaushik;

import java.io.IOException;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mycrmprkaushik.PMF;
import mycrmprkaushik.Customer;


public class CustomerServlet extends HttpServlet{

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws IOException {
	
			String name = req.getParameter("name");
	        String company = req.getParameter("company");
	        String phone = req.getParameter("phone");
	        String address = req.getParameter("address"); 
	        
	        PersistenceManager pm = PMF.get().getPersistenceManager();
	        
	        if (req.getParameterMap().containsKey("key"))
	        {       		
	        		String custkey = req.getParameter("key");
	       		    Customer c = pm.getObjectById(Customer.class, Long.parseLong(custkey));
	       		    c.setName(name);
	       		    c.setAddress(address);
	       		    c.setCompany(company);
	       		    c.setPhone(phone);   		
	       		    pm.close();
	        }
	        else {
	       		    
	       
	        Customer customer = new Customer(name,company,address,phone);
			
	        try {
	            pm.makePersistent(customer);
	        } finally {
	            pm.close();
	        }
	        
	        }
	        
	        resp.sendRedirect("/customers.jsp");
	        
	}
}
